package cn.wolfcode.luowowo.website.controller;


import cn.wolfcode.luowowo.api.cache.domain.TravelStatisVO;
import cn.wolfcode.luowowo.api.cache.prefix.TravelStatisKeyPrefix;
import cn.wolfcode.luowowo.api.cache.service.ITravelStatisRedisService;
import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.service.IDestinatonService;
import cn.wolfcode.luowowo.article.service.IGroupTravelService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.comment.domain.TravelComment;
import cn.wolfcode.luowowo.comment.service.ITravelCommentService;
import cn.wolfcode.luowowo.common.query.GroupTravelQueryObject;
import cn.wolfcode.luowowo.common.query.TravelCommentQueryObject;
import cn.wolfcode.luowowo.common.query.TravelQueryObject;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.website.annotation.RequireLogin;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/travel")
public class TravelController {
    @Reference
    private ITravelService travelService;
    @Reference
    private IDestinatonService destinatonService;
    @Reference
    private IStrategyDetailService strategyDetailService;
    //所有的旅游日记
    @Reference
    private ITravelCommentService travelCommentService;

    @Reference
    private ITravelStatisRedisService travelStatisRedisService;


    @Reference
    private IGroupTravelService groupTravelService;
    @RequestMapping("")
    public String list(Model model, @ModelAttribute("qo") TravelQueryObject qo) {
        qo.setState(Travel.STATE_RELEASE);
        //共享pageInfo过去
        PageInfo pageInfo = travelService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "travel/list";
    }

    @RequestMapping("detail")
    public String detail(Model model, Long id) {
        //共享detail到页面
        Travel detail = travelService.getById(id);
        TravelContent content = travelService.getContent(id);
        detail.setTravelContent(content);
        model.addAttribute("detail", detail);
        //共享toasts到页面
        List<Destination> toasts = destinatonService.getToasts(detail.getDest().getId());
        toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts", toasts);
        //共享阅读量前三的攻略明细
        List<StrategyDetail> sds = strategyDetailService.getDetailsByDestId(detail.getDest().getId());
        model.addAttribute("sds", sds);
        //共享阅读量前三的游记
        List<Travel> ts = travelService.getTravelTop3(detail.getDest().getId());
        model.addAttribute("ts", ts);

        //评论列表查询
        TravelCommentQueryObject travelCommentQueryObject = new TravelCommentQueryObject();
        travelCommentQueryObject.setTravelId(id);
        travelCommentQueryObject.setPageSize(Integer.MAX_VALUE);
        Page<TravelComment> page = travelCommentService.query(travelCommentQueryObject);
        List<TravelComment> contentList = page.getContent();
        model.addAttribute("list", contentList);

        TravelStatisVO statisVo = travelStatisRedisService.getStatisVo(id);
        model.addAttribute("vo",statisVo);
        //统计阅读量,每进入一次,数量+1
        travelStatisRedisService.viewIncrease(TravelStatisKeyPrefix.TRAVEL_STATIS_VO,id);


        return "travel/detail";
    }

    //写游记input
    @RequireLogin
    @RequestMapping("/input")
    public String input(Model model, Long id) {
        //判断id是否为null
        if (id != null) {
            Travel detail = travelService.getById(id);
            TravelContent content = travelService.getContent(id);
            detail.setTravelContent(content);
            model.addAttribute("tv", detail);
        }
        //查询所有的目的地共享到页面上
        List<Destination> list = destinatonService.selectAllDest(3);
        model.addAttribute("dests", list);
        return "travel/input";
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Travel travel, @UserParam UserInfo userInfo) {
        //设置作者
        travel.setAuthor(userInfo);
        //保存或者更新
        Long travelId = travelService.saveOrUpdate(travel);
        return new AjaxResult().addData(travelId);
    }

    @RequestMapping("/author")
    @ResponseBody
    public Object author(Long authorId, @UserParam UserInfo userInfo) {
        if (userInfo != null) {
            Long id = userInfo.getId();
            if (authorId.equals(id)) {
                return new AjaxResult(true, "");
            }
        }
        return new AjaxResult(false, "");
    }

    @RequestMapping("/author1")
    @ResponseBody
    public Object author1(@UserParam UserInfo userInfo) {

        return userInfo;
    }

    //游记评论
    @RequestMapping("/commentAdd")
    @RequireLogin
    public String commentAdd(Model model, TravelComment comment, @UserParam UserInfo userInfo) {
        comment.setUserId(userInfo.getId());
        comment.setHeadUrl(userInfo.getHeadImgUrl());
        comment.setLevel(userInfo.getLevel());
        comment.setCity(userInfo.getCity());
        comment.setUsername(userInfo.getNickname());
        TravelComment c = travelCommentService.save(comment);
        model.addAttribute("c", c);
        // model.addAttribute("floor", floor + 1);

        //评论数量
        travelStatisRedisService.replyIncrease(TravelStatisKeyPrefix.TRAVEL_STATIS_VO,comment.getTravelId());

        return "travel/commentTpl";
    }

    //跟团游
    @RequestMapping("/groupTravel")
    public String groupTravel(Model model, @ModelAttribute("qo") GroupTravelQueryObject qo){
        //共享pageInfo过去
        PageInfo pageInfo = groupTravelService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        //共享本周热卖过去
       List<GroupTravel> top5Sales = groupTravelService.selectTop5Sales();
       model.addAttribute("top5Sales",top5Sales);
        return "travel/goToTravel";
    }
    //根据条件查询
    @RequestMapping("/groupTravelByCondition")
    public String groupTravelByCondition(Model model, @ModelAttribute("qo") GroupTravelQueryObject qo){
        //共享pageInfo过去
        PageInfo pageInfo = groupTravelService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "travel/travelTpl";
    }


    //景点详细信息
    @RequestMapping("/freedom")
    public String freedom(Model model, @ModelAttribute("qo") GroupTravelQueryObject qo){
        qo.setPageSize(4);
        //共享本周热卖过去
        List<GroupTravel> top5Sales = groupTravelService.selectTop5Sales();
        model.addAttribute("top5Sales",top5Sales);
        return "travel/freedom";
    }

    @RequestMapping("/favor")
    @ResponseBody
    public Object favor(Long sid,@UserParam UserInfo userInfo){
        if(userInfo  == null){
            return new AjaxResult("请先登录",AjaxResult.CODE_TYPE_UNLOGIN);
        }
        //站在用户的角度
        boolean ret =  travelStatisRedisService.Userfavor(TravelStatisKeyPrefix.USER_TRAVEL_STATIS_FAVOR,sid,userInfo.getId());
        return new AjaxResult(ret,"").addData(travelStatisRedisService.getStatisVo(sid));
    }

}
